package code.c01_introduction.classwork;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/9 16:00
 * @package protect.week02
 * @description 计算序号为 i 的学生分数
 */

public class Student {

    public static void main(String[] args) {

        // ================================  测试顺序存储学生类  ================================
        Stud1[] stud1s = new Stud1[8];
        stud1s[0] = new Stud1(2018001, "王华", 90);
        stud1s[1] = new Stud1(2018002, "算法", 60);
        stud1s[2] = new Stud1(2018003, "奋斗", 97);
        stud1s[3] = new Stud1(2018004, "肯定", 98);
        stud1s[4] = new Stud1(2018005, "临时", 94);
        stud1s[5] = new Stud1(2018006, "就是", 92);
        stud1s[6] = new Stud1(2018007, "李四", 99);
        stud1s[7] = new Stud1(2018008, "介绍", 30);

        // 测试获取指定下标的学生分数
        System.out.println(Findi(stud1s, 7));

        // ================================  测试链式存储学生类  ================================
        Stud2 head;

        Stud2 s1 = new Stud2(2018001, "王华", 90);
        Stud2 s2 = new Stud2(2018002, "算法", 60);
        Stud2 s3 = new Stud2(2018003, "奋斗", 97);
        Stud2 s4 = new Stud2(2018004, "肯定", 98);
        Stud2 s5 = new Stud2(2018005, "临时", 94);
        Stud2 s6 = new Stud2(2018006, "就是", 92);
        Stud2 s7 = new Stud2(2018007, "李四", 99);
        Stud2 s8 = new Stud2(2018008, "介绍", 30);

        // 使用链式存储
        head = s1;
        s1.next = s2;
        s2.next = s3;
        s3.next = s4;
        s4.next = s5;
        s5.next = s6;
        s6.next = s7;
        s7.next = s8;
        s8.next = null;

        // 测试获取指定下标的学生分数
        System.out.println(Findi(head, 5));
    }

    /**
     * 求序号为 i 的学生分数(顺序表)
     *
     * @param stud1s 学生数组
     * @param i      学生索引
     * @return 学生分数
     */
    public static int Findi(Stud1[] stud1s, int i) {
        if (i < 0 || i > stud1s.length) {
            throw new IllegalArgumentException("参数i错误");
        } else {
            return stud1s[i].score;
        }
    }

    /**
     * 求序号为 i 的学生分数(链式表)
     *
     * @param head 链式表的头节点位置
     * @param i    学生的下表
     * @return 学生分数
     */
    public static int Findi(Stud2 head, int i) {
        // 定义一个计数器
        int count = 0;
        Stud2 p = head;
        while (count < i && p != null){
            count++;
            p = p.next;
        }
        if (i < 0 || p == null){
            throw  new IllegalArgumentException("参数i错误");
        }else {
            return p.score;
        }
    }

}

/**
 * 顺序存储的学生类
 */
class Stud1 {
    // 学号
    int no;

    // 姓名
    String name;

    // 分数
    int score;

    // 学生的构造方法
    public Stud1(int no, String name, int score) {
        this.no = no;
        this.name = name;
        this.score = score;
    }
}

class Stud2 {
    // 学号
    public int no;

    // 姓名
    public String name;

    // 分数
    public int score;

    // 只想下一个节点
    public Stud2 next;

    public Stud2(int no, String name, int score) {
        this.no = no;
        this.name = name;
        this.score = score;
    }
}
